<?php
require_once 'inc/fbapp.php';
require_once 'lib/WebServices/Proxy.php';

try{
  switch($_POST[op]) {
    case 'lyrics':
      if((isset($_POST[artist]) && isset($_POST[title])) || isset($_POST[hid])){
        include_once "lib/LeosLyrics.php";
  
        $ws = new LeosLyrics();

        if(isset($_POST[hid]))
          $ws->get_lyric_text($_POST[hid]);
        else
          $ws->search(rawurlencode($_POST[artist]), rawurlencode($_POST[title]));
    
        $ws->send();
        exit();
      }
    break;
    case 'covers':
      if(isset($_POST[artist]) && isset($_POST[title])){
        include_once "lib/WebServices/AmazonCovers.php"; 
        $ws = new AmazonCovers();
        if(isset($_POST[asin])){ $s2 = isset($_POST[asin]); $ws->setAsin($_POST[asin]); }
        $s1 =rawurlencode($_POST[artist]); $s2= rawurlencode($_POST[title]);
      }
    break;
    case 'cache_covers':
      if(isset($_POST[artist]) && isset($_POST[title]) && isset($_POST[src])){
        include_once "lib/cover_cache.php";
        exit();
      }
    break;
    case 'videos':
      if(isset($_POST[artist]) && isset($_POST[title])){
        include_once "lib/WebServices/YouTubeVids.php";
        $ws = new YouTubeVids();
        $s1 =rawurlencode($_POST[artist]); $s2= rawurlencode($_POST[title]);
      }
    break;
    case 'link_check':
      if(isset($_POST[url])){
        include_once "lib/CheckUrl.php";
        $ws = new CheckUrl($_POST[url]);       
      }
    break;
    case 'brainz':
      include_once "lib/WebServices/MusicBrainz.php"; 
      $ws = new MusicBrainz();
      if(isset($_POST[artist]) && isset($_POST[title])){
        if(isset($_POST[track_id])){ $ws->setTrackId($_POST[track_id]); }
        if(isset($_POST[rel_id])){ $ws->setMethod('release'); $ws->setReleaseId($_POST[rel_id]); }
        if(isset($_POST[artist_id])){ $ws->setMethod('artist'); $ws->setArtistId($_POST[artist_id]); }
        $s1 = $_POST[artist]; $s2 = $_POST[title];
      }
    break;
    case 'fav':
      if(isset($_POST['track_id'])){
        include_once "models/User2Fav.php";
        try{
          $user2fav = new User2Fav_(array(track_id=>$_POST['track_id'], user_id=> Session::factory()->getUserId()));
          if(isset($_POST['unfav'])){
          	$user2fav->delete(); 
          }
          else{
            $user2fav->add();
          }
          Proxy::send('<proxy id="response"><result>OK</result></proxy>');          
        }
        catch(Exception $e){
          Proxy::send('<proxy id="response"><error>'.$e->getMessage().'</error></proxy>');
        }
      }
      exit();
    break;
  }

  if(isset($_POST[op])){
    $ws->search($s1,$s2);
    $ws->sendResult();
  }
}
catch(Exception $e){ 
     header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
     header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
     header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
     header ("Pragma: no-cache"); // HTTP/1.0
     header ("Content-Type: text/xml");
     echo '<proxyerror>'.$e->getMessage().' - '.$s1.$s2.'</proxyerror>';
}

?>
